package li.com.springaidemo.timing;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import io.netty.util.internal.StringUtil;
import jakarta.annotation.PostConstruct;
import li.com.springaidemo.controller.OssController;
import li.com.springaidemo.mapper.*;
import li.com.springaidemo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
public class OssTiming {
    private static AIService aiService;
    private static BaseService baseService;
    private static HistoryService historyService;
    private static ImgsService imgsService;
    private static UserService userService;
    private static String bucketName = "ai-chunshuyingliang";
    public OssTiming(AIService aiService, BaseService baseService, HistoryService historyService, ImgsService imgsService, UserService userService) {
        OssTiming.aiService = aiService;
        OssTiming.baseService = baseService;
        OssTiming.historyService = historyService;
        OssTiming.imgsService = imgsService;
        OssTiming.userService = userService;
    }

//    执行删除图片的方法
    public static void timingDeleteImg(){
        List<String> aiMapperAllImg = aiService.list().stream().map(item -> item.getImg()).collect(Collectors.toList());
        List<String> backAllImg = aiService.list().stream().map(item -> item.getBackimg()).collect(Collectors.toUnmodifiableList());
        List<String> baseTableMapperAllImg = baseService.list().stream().map(item -> item.getImg()).collect(Collectors.toList());
        List<String> historyMapperAllImg = historyService.list().stream().map(item -> item.getImg()).collect(Collectors.toList());
        List<String> imgsMapperAllImg = imgsService.list().stream().map(item -> item.getUrl()).collect(Collectors.toList());
        List<String> userMapperAllImg = userService.list().stream().map(item -> item.getImg()).collect(Collectors.toList());
        List<String> list=new ArrayList<>();
        list.addAll(aiMapperAllImg);
        list.addAll(backAllImg);
        list.addAll(baseTableMapperAllImg);
        list.addAll(historyMapperAllImg);
        list.addAll(imgsMapperAllImg);
        list.addAll(userMapperAllImg);
//        去重
        HashSet<String> set=new HashSet<>(list);
        List<String> listEnd=new ArrayList<>(set);
//        截取字符串(每一项都删掉这段字符串)
        String str="https://ai-chunshuyingliang.oss-cn-beijing.aliyuncs.com/";
        for(int i=0;i<listEnd.size();i++){
            String originalString = listEnd.get(i);
            String modifiedString = originalString.replace(str, "");
            listEnd.set(i, modifiedString);
        }

        // 设置请求参数
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
        // 查询对象存储空间所有的图片
        List<OSSObjectSummary> ossList = OssController.ossClient.listObjects(listObjectsRequest).getObjectSummaries();
        // 求差值，找到要删除的图片
        List<String> deleteList = ossList
                .stream()
                .map(item -> item.getKey())
                .filter(item ->{
                    if(!listEnd.contains(item)){
                        return true;
                    }else {
                        return false;
                    }
                })
                .collect(Collectors.toList());
        //创建删除请求
        DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName);
        if(deleteList.size()>0){
            deleteRequest.setKeys(deleteList);
    //        批量删除
            OssController.ossClient.deleteObjects(deleteRequest);
        }
    }

//    测试定时任务能不能执行
    public static void test1(){
        System.out.println(1);
        System.out.println("oss:"+OssController.ossClient);
        System.out.println("ai:"+aiService);
    }
    public static void test2(){
        System.out.println(2);
    }
}
